﻿/*****************************************************************************
 * Hyldahl.Services.TaskProcessing.WindowsService  Created by: MSH 2008.07.22
 * --------------------------------------------------------------------------
 * FileName: ProjectInstaller.cs
 * --------------------------------------------------------------------------
 * Copyright 2009 Martin Sixhøj Hyldahl
 *****************************************************************************/

using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;
using Microsoft.Win32;

namespace Hyldahl.Services.TaskProcessing.WindowsService
{
    [RunInstaller(true)]
    public partial class ProjectInstaller : Installer
    {
        /*****************************************************
         * FIELDS
         *****************************************************/
        private string serviceInstanceName;

        /// <summary>
        /// Initializes a new instance of the <see cref="ProjectInstaller"/> class.
        /// </summary>
        public ProjectInstaller()
        {
            InitializeComponent();
        }

        public override void Install(IDictionary stateSaver)
        {
            GetServiceInstanceName();

            base.Install(stateSaver);
        }

        public override void Uninstall(IDictionary savedState)
        {
            GetServiceInstanceName();

            base.Uninstall(savedState);
        }

        private void GetServiceInstanceName()
        {
            //serviceInstanceName = Context.Parameters["ServiceInstanceName"];

            //if (string.IsNullOrEmpty(serviceInstanceName))
            //    throw new Exception("Instance name was not recieved.");

            //taskProcessingServiceInstaller.DisplayName = string.Format("Hyldahl Task Processing Service ({0})", serviceInstanceName);
            //taskProcessingServiceInstaller.ServiceName = string.Format("HyldahlTaskProcessingService ({0})", serviceInstanceName);
        }


        /// <summary>
        /// Raises the <see cref="E:System.Configuration.Install.Installer.AfterInstall"/> event.
        /// </summary>
        /// <param name="savedState">An <see cref="T:System.Collections.IDictionary"/> that contains the state of the computer after all the installers contained in the <see cref="P:System.Configuration.Install.Installer.Installers"/> property have completed their installations.</param>
        protected override void OnAfterInstall(IDictionary savedState)
        {
            // Set allow interact with desktop

            RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\HyldahlTaskProcessingService", true);

            if (key != null && key.GetValue("Type") != null)
            {
                key.SetValue("Type", ((int)key.GetValue("Type") | 256));
            }


            base.OnAfterInstall(savedState);
        }
    }
}
